//
//  ActivityViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/2/2.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityModel.h"
@interface ActivityViewController ()

@end

@implementation ActivityViewController
#pragma mark - 视图的生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    
    [self addBackItem];
    
    self.navigationItem.title = @"主题活动";
    
        // Do any additional setup after loading the view.
}
-(void)requestData
{
    self.view.backgroundColor = [UIColor whiteColor];
    __weak typeof(self)weakSelf = self;
    [U76HONet commendActivityWithAction:@"" Infoid:_infoId andDataBlock:^(ActivityModel *model) {
        NSLog(@"推荐活动获取数据成功了");
        [weakSelf mainwithactivitymodel:model];
    }];
}
-(void)mainwithactivitymodel:(ActivityModel *)model
{
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:scrollView];
    
    //标题
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(20), SIZETO6W(20), SCREEN_WIDTH-SIZETO6W(20)*2, 30)];
    titleLab.text = model.title;
    titleLab.textColor = UIColorFromRGB(0x333944);
    titleLab.font = [UIFont systemFontOfSize:18];
    [scrollView addSubview:titleLab];
    
    //时间
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString * dataCh = model.addtime;
    NSString * dataStr = [dataCh substringToIndex:dataCh.length];
    NSDate * data = [[NSDate dateWithTimeIntervalSince1970:[dataStr integerValue]] dateByAddingTimeInterval:8*3600];
    NSString * lastStr = [NSString stringWithFormat:@"%@",data];
    NSArray * lastArr = [lastStr componentsSeparatedByString:@" "];
    UILabel * dateLab = [[UILabel alloc]initWithFrame:CGRectMake(titleLab.left, titleLab.bottom+SIZETO6H(10), 120, 20)];
    dateLab.text = lastArr[0];
    dateLab.textColor = UIColorFromRGB(0x616978);
    [scrollView addSubview:dateLab];
    
    NSArray * PicArray;//活动图片数组
    
    
    if (![model.pic isEqual:@""]) {
        
        PicArray = [model.pic componentsSeparatedByString:@"|"];
        
    }
    UIImageView * TiDaiImageView = [[UIImageView alloc]initWithFrame:CGRectMake(titleLab.left, dateLab.bottom+SIZETO6H(30), 0, 0)];//替代View

   
    
    for (int i=0; i<PicArray.count; i++) {
        
        
        //图片
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(titleLab.left, dateLab.bottom+SIZETO6H(30)*i, SCREEN_WIDTH-SIZETO6W(20)*2, (SCREEN_WIDTH-SIZETO6W(20)*2)/4*3)];
        [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,PicArray[i]]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
        [scrollView addSubview:imgV];
        
        TiDaiImageView = imgV;
        
        
    }
    
    

    
    
    //内容
    NSAttributedString * Str = [[NSAttributedString alloc] initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    NSString * str = [Str string];

    CGFloat height = [str heightOfSize:18 width:SCREEN_WIDTH-SIZETO6W(20)*2];
    
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8.0];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
    
    UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(titleLab.left, TiDaiImageView.bottom+SIZETO6H(10), SCREEN_WIDTH-SIZETO6W(20)*2, height)];
    contentLab.numberOfLines = 0;
    contentLab.font = [UIFont systemFontOfSize:15];
    contentLab.textColor = UIColorFromRGB(0x616978);
    contentLab.attributedText = attributedString;
    [scrollView addSubview:contentLab];
    
   // contentLab.bottom = scrollView.bottom;
    
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, contentLab.bottom);
}

#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    if (_isAPNS) {
        AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        self.view.window.rootViewController = app.viewController;
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
